package com.elinkway.infinitemovies.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.utils.AdStatisticsSp;
import com.elinkway.infinitemovies.utils.HostUtil;
import com.elinkway.infinitemovies.utils.MyLocationManager;
import com.elinkway.infinitemovies.utils.SharePreferenceManager;
import com.elinkway.infinitemovies.utils.ToastUtil;
import com.elinkway.infinitemovies.utils.Utils;
import com.le123.ysdq.R;

import cn.jpush.android.api.JPushInterface;

public class EnvironmentSwitchActivity extends BaseSecondaryActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    public static final String TAG = "EnvironmentSwitchActivity";
    private RadioGroup mEnvGroup;
    private RadioButton mEvnLineOnHttps, mEnvLineOff, mUserDefine, mOutAli, mOutUcloud, mOutUcloudTest;
    private Button mLocationSure, mDefineSure;
    private EditText mLoacation, mDefineIp;
    private RelativeLayout mUserDefineContainer;
    private EditText jpshEt;
    private TextWatcher mLocationWatch, mDefineIpWatch;
    private TextView mLocation_city_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Utils.isMonkeyRunning()) {
            finish();
            return;
        }
        super.setActionBarActivityClassName(TAG);
        setContentView(R.layout.activity_environment_switch);
        initActionBar();
        mTitleTv.setText("环境协议切换");
        initTextWatch();
        initView();
        setListener();
    }

    private void initView() {
        mEnvGroup = (RadioGroup) findViewById(R.id.Environment_select);
        mEvnLineOnHttps = (RadioButton) findViewById(R.id.line_on_https_env);
        mOutAli = (RadioButton) findViewById(R.id.out_ali_test_env);
        mOutUcloud = (RadioButton) findViewById(R.id.out_ucloud_env);
        mOutUcloudTest = (RadioButton) findViewById(R.id.out_ucloud_test_env);
        mUserDefine = (RadioButton) findViewById(R.id.user_define);
        mEnvLineOff = (RadioButton) findViewById(R.id.line_off_env);
        mLocationSure = (Button) findViewById(R.id.location_sure);
        mDefineSure = (Button) findViewById(R.id.define_sure);
        mDefineIp = (EditText) findViewById(R.id.define);
        mLoacation = (EditText) findViewById(R.id.location);
        mLocation_city_text = (TextView) findViewById(R.id.location_city_text);
        mUserDefineContainer = (RelativeLayout) findViewById(R.id.user_define_container);
        jpshEt = (EditText) findViewById(R.id.jpsh_et);
        jpshEt.setText(JPushInterface.getRegistrationID(this));
        String hostValue = SharePreferenceManager.getVaule(this, MoviesHttpApi.TEST_HOST, "");
        if (TextUtils.isEmpty(hostValue) || HostUtil.OnlineHost.PRODUCT_HOST.equals(hostValue)) {
            mEvnLineOnHttps.setChecked(true);
            mUserDefineContainer.setVisibility(View.GONE);
        } else if (HostUtil.TestHost.PRODUCT_HOST.equals(hostValue)) {
            mEnvLineOff.setChecked(true);
            mUserDefineContainer.setVisibility(View.GONE);
        } else if (HostUtil.AliHost.PRODUCT_HOST.equals(hostValue)) {
            mOutAli.setChecked(true);
            mUserDefineContainer.setVisibility(View.GONE);
        } else if (HostUtil.UcloudHost.PRODUCT_HOST.equals(hostValue)) {
            mOutUcloud.setChecked(true);
            mUserDefineContainer.setVisibility(View.GONE);
        } else if (HostUtil.UcloudTestHost.PRODUCT_HOST.equals(hostValue)) {
            mOutUcloudTest.setChecked(true);
            mUserDefineContainer.setVisibility(View.GONE);
        } else { //自定义
            mUserDefine.setChecked(true);
            mDefineIp.setText(SharePreferenceManager.getVaule(
                    this, MoviesHttpApi.TEST_HOST, ""));
            mUserDefineContainer.setVisibility(View.VISIBLE);
        }

        if(!TextUtils.isEmpty(SharePreferenceManager.getVaule(
                this, MyLocationManager.LOCATIONKEY, ""))) {
            mLoacation.setText(SharePreferenceManager.getVaule(
                    this, MyLocationManager.LOCATIONKEY, ""));
        }
        TextView baidusplashadresponse = (TextView) findViewById(R.id.baidu_splashad_reponse);
        TextView baidusplashadrequest = (TextView) findViewById(R.id.baidu_splashad_request);
        TextView baidusplashadsuccess = (TextView) findViewById(R.id.baidu_splashad_success);
        TextView baidusplashaderror = (TextView) findViewById(R.id.baidu_splashad_error);
        TextView baidusplashadshow = (TextView) findViewById(R.id.baidu_splashad_show);
        baidusplashadresponse.setText("开屏百度广告类型返回次数: " + AdStatisticsSp.getVaule(this, AdStatisticsSp.BAIDUSPLASHADRESPONSE));
        baidusplashadrequest.setText("开屏百度广告请求次数: "+AdStatisticsSp.getVaule(this, AdStatisticsSp.BAIDUSPLASHADREQUEST));
        baidusplashadsuccess.setText("开屏百度广告返回成功次数: "+AdStatisticsSp.getVaule(this, AdStatisticsSp.BAIDUSPLASHADSUCCESS));
        baidusplashaderror.setText("开屏百度广告出错次数: "+AdStatisticsSp.getVaule(this, AdStatisticsSp.BAIDUSPLASHADERROR));
        baidusplashadshow.setText("开屏百度广告展示次数: "+AdStatisticsSp.getVaule(this, AdStatisticsSp.BAIDUSPLASHADSHOW));

        TextView baiduexitadresponse = (TextView) findViewById(R.id.baidu_exitad_reponse);
        TextView baiduexitadrequest = (TextView) findViewById(R.id.baidu_exitad_request);
        TextView baiduexitadsuccess = (TextView) findViewById(R.id.baidu_exitad_success);
        TextView baiduexitaderror = (TextView) findViewById(R.id.baidu_exitad_error);
        TextView baiduexitadshow = (TextView) findViewById(R.id.baidu_exitad_show);
        baiduexitadresponse.setText("退出百度广告类型返回次数: " + AdStatisticsSp.getVaule(this, AdStatisticsSp.BAIDUEXITADRESPONSE));
        baiduexitadrequest.setText("退出百度广告请求次数: "+AdStatisticsSp.getVaule(this, AdStatisticsSp.BAIDUEXITADREQUEST));
        baiduexitadsuccess.setText("退出百度广告返回成功次数: "+AdStatisticsSp.getVaule(this, AdStatisticsSp.BAIDUEXITADSUCCESS));
        baiduexitaderror.setText("退出百度广告出错次数: "+AdStatisticsSp.getVaule(this, AdStatisticsSp.BAIDUEXITADERROR));
        baiduexitadshow.setText("退出百度广告展示次数: "+AdStatisticsSp.getVaule(this, AdStatisticsSp.BAIDUEXITADSHOW));
        String city = SharePreferenceManager.getVaule(this, MyLocationManager.LOCATION_CITY, "");
        mLocation_city_text.setText(city);

    }

    private void initTextWatch() {
        mLocationWatch = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };

        mDefineIpWatch = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
    }
    private void setListener() {
        mEnvGroup.setOnCheckedChangeListener(this);
        mDefineIp.addTextChangedListener(mDefineIpWatch);
        mLoacation.addTextChangedListener(mLocationWatch);
        mDefineSure.setOnClickListener(this);
        mLocationSure.setOnClickListener(this);
    }

    public static void launch(Context context) {
        Intent intent = new Intent(context, EnvironmentSwitchActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getCheckedRadioButtonId()) {
            case R.id.line_on_https_env:
                SharePreferenceManager.saveVaule(this, MoviesHttpApi.TEST_HOST, HostUtil.OnlineHost.PRODUCT_HOST);
                mUserDefineContainer.setVisibility(View.GONE);
                break;
            case R.id.line_off_env:
                SharePreferenceManager.saveVaule(this, MoviesHttpApi.TEST_HOST, HostUtil.TestHost.PRODUCT_HOST);
                mUserDefineContainer.setVisibility(View.GONE);
                break;
            case R.id.user_define:
                mUserDefineContainer.setVisibility(View.VISIBLE);
                break;
            case R.id.out_ali_test_env:
                SharePreferenceManager.saveVaule(this, MoviesHttpApi.TEST_HOST, HostUtil.AliHost.PRODUCT_HOST);
                mUserDefineContainer.setVisibility(View.GONE);
                break;
            case R.id.out_ucloud_env:
                SharePreferenceManager.saveVaule(this, MoviesHttpApi.TEST_HOST, HostUtil.UcloudHost.PRODUCT_HOST);
                mUserDefineContainer.setVisibility(View.GONE);
                break;
            case R.id.out_ucloud_test_env:
                SharePreferenceManager.saveVaule(this, MoviesHttpApi.TEST_HOST, HostUtil.UcloudTestHost.PRODUCT_HOST);
                mUserDefineContainer.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.location_sure:
                String inputLoc = mLoacation.getText().toString();
                if(!"".equals(inputLoc)) {
                    SharePreferenceManager.saveVaule(this, MyLocationManager.LOCATIONKEY,
                            inputLoc);
                    SharePreferenceManager.saveVaule(this, MyLocationManager.HAS_FIXED_LOCATION, "1");
                    ToastUtil.showShortToast(EnvironmentSwitchActivity.this, "地域 已改为:" + inputLoc);
                }

                break;
            case R.id.define_sure:
                String inputIp = mDefineIp.getText().toString();
                if(!"".equals(inputIp)) {
                    SharePreferenceManager.saveVaule(this, MoviesHttpApi.TEST_HOST,
                            "http://" + inputIp + "/");
                    ToastUtil.showShortToast(EnvironmentSwitchActivity.this, "ip 已改为:" + inputIp);
                }
        }
    }
}
